#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.59)
AC_INIT([pf_lib], [1.0.0], [yanghz@ucweb.local])
AM_INIT_AUTOMAKE([foreign -Wall -Werror])
AC_CONFIG_SRCDIR([source/pf_arg.c])
AC_CONFIG_HEADER([config.h])

AC_ARG_ENABLE(debug, [AC_HELP_STRING([--enable-debug], [compile for debugging])], enable_debug=$enableval, enable_debug=no)
if test "$enable_debug" = "yes"; then
    CFLAGS="-g -O0 -DDEBUG"
    AC_DEFINE(DEBUG,1,[define to enable debugging code])
else
    CFLAGS="-g -O2 -DNDEBUG -minline-all-stringops -fomit-frame-pointer -Wuninitialized"
fi

# store current user givern compiler flags to avoid default setup via AC_PROG_CXX
OLD_CFLAGS=$CFLAGS

# Checks for programs.
AC_PROG_CXX
AC_PROG_CC
AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PROG_LIBTOOL

# reset compiler flagsto initial flags
CFLAGS=$OLD_CFLAGS

#MIN_VERSION=4.1.2
#AC_MSG_CHECKING([$CC version])
#CC_VER=`$CC -dumpversion`
#is_ok=`echo $MIN_VERSION.$CC_VER | awk 'BEGIN{FS="."}{print(($4>$1)||(($4==$1)&&(($5>$2)||(($5==$2)&&($6>=$3))))) ? "OK" : "FAILED";}'`
#if test "$is_ok" = "OK"; then
#	AC_MSG_RESULT([$CC_VER]) 
#else
#	AC_MSG_RESULT([$CC_VER]) 
#	AC_MSG_ERROR([ !!! we need $CC version up to $MIN_VERSION !!! ]) 
#fi

AC_ARG_ENABLE(gcov, [AC_HELP_STRING([--enable-gcov], [compile for gcov])], enable_gcov=$enableval, enable_gcov=no)
if test "$enable_gcov" = "yes"; then
	if test "$enable_debug" = "yes"; then
		CFLAGS="$CFLAGS -fprofile-arcs -ftest-coverage"
		LDFLAGS="$LDFLAGS -lgcov "
	else
		AC_MSG_ERROR([ !!! debug must be enabled when try to enable gcov !!! ]) 
	fi
fi

AC_ARG_WITH([gtest], [AS_HELP_STRING([--with-gtest=DIR],[Is build for gtest])],,[withval=$default_find_path])
if test -n "$withval"; then
    AC_MSG_CHECKING([for gtest $withval]) 
    if test "$enable_debug" != "yes" -o "$enable_gcov" != "yes" ; then 
        AC_MSG_RESULT([no debug or gcov enable]) 
        AC_MSG_ERROR([enable gtest must enable debug and gcov first]) 
    fi 
    for dir in "." $withval/include ; do 
        if test -f "$dir/gtest/gtest.h" ; then 
            CFLAGS="$CFLAGS -I$dir" 
            gtest_include="ok"
        fi
    done
    for dir in "." $withval/lib ; do 
        if test -f "$dir/libgtest.la" ; then 
            GTEST_LIBS="$dir/" 
            gtest_lib="ok"
        fi
    done
    if test "$gtest_include" == "ok" -a "$gtest_lib" == "ok" ; then 
        CFLAGS="$CFLAGS -DUNIT_TEST" 
        AC_MSG_RESULT([found]) 
    else 
        AC_MSG_RESULT([not found]) 
        AC_MSG_ERROR([gtest not found]) 
    fi 
fi
if test -n "$GTEST_LIBS"; then
    AM_CONDITIONAL([GTEST], [true])
    AC_SUBST(GTEST_LIBS)
else
    AM_CONDITIONAL([GTEST], [false])
fi

AC_ARG_ENABLE(valgrind-check, [AC_HELP_STRING([--enable-valgrind-check], [memcheck for valgrind])], enable_valgrind_check=$enableval, enable_valgrind_check=no)
if test "$enable_valgrind_check" = "yes"; then
	CFLAGS="$CFLAGS -DVALGRIND_CHECK"
fi

CFLAGS="$CFLAGS -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64"
gcc_arg_list="-pipe -fstack-protector-all -freg-struct-return -fno-strict-aliasing -Wno-write-strings -Wall -Wextra -Wparentheses -Winline -Wunused -Wcomment -Wformat -Wimplicit -Wsequence-point -Wfloat-equal -Wshadow -Werror -Wmissing-include-dirs -Wstrict-overflow "
for arg in $gcc_arg_list; do
    AX_CHECK_COMPILER_FLAGS(["$arg"], [CFLAGS="$CFLAGS $arg"]) 
done

AC_ARG_WITH(gcc-arch, [AS_HELP_STRING([--with-gcc-arch=<arch>], [use architecture <arch> for gcc -march/-mtune, instead of guessing])], gcc_arch_list=$withval, gcc_arch_list=sys)
if test "$gcc_arch_list" = "sys" ; then
	case $host_cpu in
		x86_64*) gcc_arch_list="native corei7 core2 nocona pentium4 pentiumpro i686" ;;
		*) gcc_arch_list="native prescott pentium4 pentium-m pentium3 pentium2 pentiumpro i686" ;;
	esac
fi
for arch in $gcc_arch_list; do
	AX_CHECK_COMPILER_FLAGS(["-march=$arch"], [CFLAGS="$CFLAGS -march=$arch"; break]) 
done

CXXFLAGS="$CFLAGS -D__STDC_FORMAT_MACROS -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS"
CFLAGS="$CFLAGS --std=gnu99"

# Checks for libraries.
# FIXME: Replace `main' with a function in `-lpthread':
AC_CHECK_LIB([pthread], [main])
# FIXME: Replace `main' with a function in `-lrt':
AC_CHECK_LIB([rt], [main])
# FIXME: Replace `main' with a function in `-lm':
AC_CHECK_LIB([m], [main])

# Checks for header files.
AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS([arpa/inet.h fcntl.h float.h inttypes.h limits.h memory.h netdb.h netinet/in.h stddef.h stdint.h stdlib.h string.h strings.h sys/file.h sys/param.h sys/socket.h sys/time.h syslog.h unistd.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_TYPE_MODE_T
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_STRUCT_TM
AC_HEADER_STDBOOL
AC_C_VOLATILE
AC_C_BIGENDIAN
AC_CHECK_TYPES([ptrdiff_t])

# Checks for library functions.
AC_FUNC_FORK
AC_FUNC_FSEEKO
AC_FUNC_MALLOC
AC_FUNC_MEMCMP
AC_FUNC_REALLOC
AC_FUNC_VPRINTF
AC_CHECK_FUNCS([clock_gettime dup2 floor getcwd gettimeofday localtime_r memmove memset mkdir socket strchr strdup strerror strncasecmp strstr strtol])

AC_CONFIG_FILES([Makefile
                 include/Makefile
                 source/Makefile
                 tests/Makefile
                 tests/app/Makefile
                 tests/gtest/Makefile])
AC_OUTPUT
